#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
学生页面
"""
import sys
import pickle
from lib.log import logger


class StudentPage(object):
    """
    学生页面
    """

    def __init__(self, name):
        self.name = name

    def check_all_courses(self):
        """
        查看所有课程
        :return:
        """
        with open('../db/course.txt', 'r', encoding='utf-8') as f:
            key = f.readline().strip()
            print(key)
            for line in f:
                value = line.strip()
                print(value)
            back = input('回车键返回：')
            return

    def selection_course(self):
        """
        选课系统
        :return:
        """
        while True:
            num = input('请输入课程序号(q返回)：')
            if num.upper() == 'Q':
                return
            flag = False
            with open(r'../db/course.txt', 'r', encoding='utf-8') as f1:
                for line in f1:
                    line_lst = line.strip().split('|')
                    if num == line_lst[0]:
                        with open(r'../db/user_course.txt', 'rb') as f2:
                            try:
                                data = pickle.load(f2)
                            except Exception as e:
                                data = dict()
                            if data.get(self.name):
                                for i in data[self.name]:
                                    flag2 = False
                                    if num == i.split('|')[0]:
                                        flag2 = True
                                        break
                                if not flag2:
                                    data[self.name].append(line.strip())
                                    logger.info('%s选择了课程' % self.name)
                                    with open(r'../db/user_course.txt', 'wb') as f3:
                                        pickle.dump(data, f3)
                                        flag = True
                            else:
                                data[self.name] = [line.strip()]
                                logger.info('%s选择了课程' % self.name)
                                with open(r'../db/user_course.txt', 'wb') as f3:
                                    pickle.dump(data, f3)
                                    flag = True
                if not flag:
                    print('无此课程序或已选此课程！')
                    continue

    def check_courses(self):
        with open(r'../db/user_course.txt', 'rb') as f:
            try:
                data = pickle.load(f)
                for i in data[self.name]:
                    i = i.split('|')
                    print(i[1], i[3], i[4])
            except Exception as e:
                print('你还没有选课，快去选课吧！')

    def student_page(self):
        print('欢迎学生 %s登录' % self.name)
        while True:
            print('##########学生用户##########\n'
                  '1.查看所有课程\n'
                  '2.选择课程\n'
                  '3.查看所选课程\n'
                  '4.退出程序')
            page_list = {
                '1': self.check_all_courses,
                '2': self.selection_course,
                '3': self.check_courses
            }
            num = input('请输入序号选择:')
            if num == '4':
                sys.exit('谢谢使用！')
            if page_list.get(num):
                page_list.get(num)()
            else:
                print('请输入正确的序号！')
                continue
